package com.xdwanj.dao.impl;

import com.xdwanj.dao.BannerDao;
import com.xdwanj.dao.BaseDao;
import com.xdwanj.pojo.Banner;
import com.xdwanj.pojo.Commodity;
import com.xdwanj.util.PageBean;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class BannerDaoImpl implements BannerDao {
    private BaseDao baseDao = new BaseDao();
    JdbcTemplate jdbcTemplate = baseDao.getJdbcTemplate();

    @Override
    public PageBean<Banner> findBannerList(int pageSize) {
        String sql = " from banner ba,commodity co where ba.id = co.id ";
        System.out.println(new StringBuffer("select *").append(sql));
        PageBean customerPageDto = baseDao.getCustomerPageDto(new StringBuffer("select *").append(sql).toString(), null, new BeanPropertyRowMapper<Banner>(Banner.class), new StringBuffer("select count(*)").append(sql).toString(), null, 1, pageSize);
        List<Commodity> commodities = jdbcTemplate.query(new StringBuffer("select *").append(sql).toString(), new BeanPropertyRowMapper<Commodity>(Commodity.class));
        System.out.println(commodities);
        List<Banner> banners = customerPageDto.getContent();
        for (Banner banner : banners) {
            for (int i = 0; i < commodities.size(); i++) {
                if (commodities.get(i).getId().equals(banner.getCom_id())) {
                    banner.setCommodity(commodities.get(i));
                }
            }
        }
        return customerPageDto;
    }
}
